package com.itheima.controller;


import com.itheima.anno.Log;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;
    /**
     * 修改密码
     */
    @PutMapping("/changePassword")
    public Result changePwd(@RequestBody Pwd pwd) {

        if (empService.changePwd(pwd)) {
            return Result.success();
        }
        return Result.error("原密码错误");
    }

    /**
     * 修改员工
     */
    @Log
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp) {
        empService.update(emp);
        return Result.success();
    }
    /**
     * 根据id回显员工信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    /**
     * 批量删除员工
     */
    @Log
    @DeleteMapping()
    public Result deleteBatch(@RequestParam List<Integer> ids) {
        empService.deleteBatch(ids);
        return Result.success();
    }


    /**
     * 根据条件查询员工列表
     *
     * @return
     */
    @GetMapping
    public Result getEmps(EmpQueryParam empQueryParam) {
        PageBean pageBean = empService.list(empQueryParam);

        return Result.success(pageBean);
    }

    /**
     * 员工新增
     */
    @Log
    @PostMapping
    public Result addEmp(@RequestBody Emp emp) {
        empService.addEmp(emp);

        return Result.success();
    }

    /**
     * 查询全部员工
     */
    @GetMapping("/list")
    public Result getEmpsList() {
        List<Emp> list = empService.findAll();
        return Result.success(list);
    }
}
